package com.game.my.api;

import java.util.Date;

import com.game.my.model.Account;
import com.jfinal.core.Controller;

public class AddMoneyApi extends Controller {

	// 充值
	public void insertMoney() {
		boolean flag = false;
		double money = Double.parseDouble(getPara("money"));
		Account account = Account.dao.getAcount(Long.parseLong("1"));
		Account acc = getModel(Account.class);
		Date rechargeTime = new Date();// 充值时间
		Date createTime = new Date(); // 创建时间
		if (account != null) {
			double balance = account.getDouble("balance");// 账户余额
			balance = balance + money;
			double totalExpenses = account.getDouble("totalExpenses");// 总支出金额
			double totalWithdraw = account.getDouble("totalWithdraw");// 总提现金额
			double withdrawMoney = account.getDouble("withdrawMoney");// 提现金额
			Date withdrawTime = account.getDate("withdrawTime");// 提现时间
			int accountFlag = 0;// 充值
			acc.set("userId", 1);
			acc.set("balance", balance);
			acc.set("totalExpenses", totalExpenses);// 总支出金额
			acc.set("totalWithdraw", totalWithdraw);// 总提现金额
			acc.set("withdrawMoney", withdrawMoney);// 提现金额
			acc.set("withdrawTime", withdrawTime);// 提现时间
			acc.set("rechargeMoney", money);// 充值金额
			acc.set("rechargeTime", rechargeTime);// 充值时间
			acc.set("accountFlag", accountFlag);// 充值还是提现
			acc.set("createTime", createTime);// 创建时间
			flag = acc.save();
		} else {
			acc.set("userId", 1);
			acc.set("balance", money);
			acc.set("totalExpenses", 0);// 总支出金额
			acc.set("totalWithdraw", 0);// 总提现金额
			acc.set("withdrawMoney", 0);// 提现金额
			// acc.set("withdrawTime", );//提现时间
			acc.set("rechargeMoney", money);// 充值金额
			acc.set("rechargeTime", rechargeTime);// 充值时间
			acc.set("accountFlag", 0);// 充值还是提现
			acc.set("createTime", createTime);// 创建时间
			flag = acc.save();
		}

		renderJson(flag);
	}

	// 提现
	public void outmoney() {
		double money = Double.parseDouble(getPara("money"));
		Account account = Account.dao.getAcount(Long.parseLong("1"));
		boolean flag = false;
		if (account != null) {
			double balance = account.getDouble("balance");// 账户余额
			if (balance >= money) {
				balance = balance - money;
				double totalExpenses = account.getDouble("totalExpenses");// 总支出金额
				double totalWithdraw = account.getDouble("totalWithdraw");// 总提现金额
				totalWithdraw = totalWithdraw + money;// 总提现金额
				double withdrawMoney = account.getDouble("withdrawMoney");// 提现金额
				Date withdrawTime = new Date();// 提现时间
				// Date rechargeTime = account.getDate("rechargeTime");//充值时间
				Date createTime = new Date(); // 创建时间
				int accountFlag = 1;// 提现
				account.set("balance", balance);// 余额
				account.set("totalExpenses", totalExpenses);// 总支出金额
				account.set("totalWithdraw", totalWithdraw);// 总提现金额
				account.set("withdrawMoney", withdrawMoney);// 提现金额
				account.set("withdrawTime", withdrawTime);// 提现时间
				account.set("rechargeMoney", 0);// 充值金额
				// account.set("rechargeTime", rechargeTime);//充值时间
				account.set("accountFlag", accountFlag);// 充值还是提现
				account.set("createTime", createTime);// 创建时间
				flag = account.save();
				renderJson(flag);
			} else {
				renderJson(flag);
			}
		} else {
			renderJson(flag);
		}
	}

}
